package com.happok.onlineclass.usermanagement.user.controller;

import com.happok.common.page.PageResponse;
import com.happok.onlineclass.usermanagement.common.dto.IdDTO;
import com.happok.onlineclass.usermanagement.user.dto.*;
import com.happok.onlineclass.usermanagement.user.service.ManageUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URISyntaxException;
import java.util.List;

/**
 * 管理员操作用户
 * 平台对用户的操作，添加，修改，查询用户列表，查询用户详情，启用禁用用户
 *
 * @author : chenac
 * @module 在线课堂/用户管理          yapi 项目组/子项目
 * @date : 2021/6/28 17:29
 */
@RestController
@Api(tags = "管理员操作用户")
@RequestMapping(value = "/user-management/management/users")
public class ManageUserController {

    @Autowired
    private ManageUserService manageUserService;

    /**
     * 添加用户
     * 邮箱与手机号必须有一个 <br>
     * 用户扩展信息添加格式如下：<br>
     * <字段英文名1>：<字段值1><br>
     * <字段英文名2>：<字段值2><br>
     * ...<br>
     * <字段英文名n>：<字段值n><br>
     * <br>
     * 20000,添加用户成功 <br>
     * 40000,参数检测异常 <br>
     * 40001,用户账号（手机或邮箱）不能为空 <br>
     * 40903,电话号码重复<br>
     * 40904,邮箱重复<br>
     * 40400,表单不存在<br>
     * 40401,字段不存在<br>
     * 20010,通知发送失败<br>
     * 50000,服务端异常
     *
     * @param addUserDTO
     * @author: chenac
     * @date: 14:46
     * @execption:
     * @return: com.happok.onlineclass.usermanagement.common.dto.IdDTO
     */
    @PostMapping(value = "/v1")
    @ApiOperation(value = "添加用户")
    @ApiResponses({
            @ApiResponse(code = 20000, message = "添加用户成功"),
            @ApiResponse(code = 40000, message = "参数检测异常"),
            @ApiResponse(code = 40001, message = "用户账号（手机或邮箱）不能为空"),
            @ApiResponse(code = 40903, message = "电话号码重复"),
            @ApiResponse(code = 40904, message = "邮箱重复"),
            @ApiResponse(code = 40400, message = "表单不存在"),
            @ApiResponse(code = 40401, message = "字段不存在"),
            @ApiResponse(code = 20010, message = "通知发送失败"),
            @ApiResponse(code = 50000, message = "服务端异常")
    })
    public IdDTO addUser(@RequestHeader String user, @RequestBody @Valid AddUserDTO addUserDTO) throws Exception {
        return manageUserService.addUser(user, addUserDTO);
    }

    /**
     * 修改用户信息
     * 修改用户信息 <br>
     * 用户扩展信息添加格式如下：<br>
     * <字段英文名1>：<字段值1><br>
     * <字段英文名2>：<字段值2><br>
     * ...<br>
     * <字段英文名n>：<字段值n><br>
     * <br>
     * 20000，修改用户信息成功 <br>
     * 40000，参数错误 <br>
     * 40402，用户不存在 <br>
     * 40903,电话号码重复<br>
     * 40904,邮箱重复<br>
     * 40400,表单不存在<br>
     * 40401,字段不存在<br>
     * 50000,服务端异常
     *
     * @param userId              修改的用户id
     * @param manageUpdateUserDTO
     * @author: chenac
     * @date: 14:58
     * @execption:
     * @return: void
     */
    @PutMapping(value = "/{userId}/v1")
    @ApiOperation(value = "修改用户信息")
    @ApiResponses({
            @ApiResponse(code = 20000, message = "修改用户信息成功"),
            @ApiResponse(code = 40000, message = "参数错误"),
            @ApiResponse(code = 40402, message = "用户不存在"),
            @ApiResponse(code = 40903, message = "电话号码重复"),
            @ApiResponse(code = 40904, message = "邮箱重复"),
            @ApiResponse(code = 40400, message = "表单不存在"),
            @ApiResponse(code = 40401, message = "字段不存在"),
            @ApiResponse(code = 50000, message = "服务端异常")
    })
    public void updateUserByUserId(@RequestHeader String user,
                                   @PathVariable("userId") Integer userId,
                                   @RequestBody @Valid ManageUpdateUserDTO manageUpdateUserDTO) throws Exception {
        manageUserService.updateUserByUserId(user, userId, manageUpdateUserDTO);
    }

    /**
     * 禁启用用户
     * 禁启用用户 <br>
     * 20000,禁启用用户成功 <br>
     * 40004,管理员用户不能被禁用 <br>
     * 40402,用户不存在
     *
     * @param userId        禁用的用户id
     * @param enableUserDTO
     * @author: chenac
     * @date: 15:06
     * @execption:
     * @return: void
     */
    @PutMapping(value = "/{userId}/enable/v1")
    @ApiOperation(value = "禁启用用户")
    @ApiResponses({
            @ApiResponse(code = 20000, message = "禁启用用户成功"),
            @ApiResponse(code = 40004, message = "管理员用户不能被禁用"),
            @ApiResponse(code = 40402, message = "用户不存在")
    })
    public void enableUserByUserId(@PathVariable("userId") Integer userId,
                                   @RequestBody EnableUserDTO enableUserDTO) {
        manageUserService.enableUserByUserId(userId, enableUserDTO);
    }

    /**
     * 检测用户账号
     * 邮箱与手机号必须有一个    <br>
     * 20000,用户账号正常<br>
     * 40000,参数错误<br>
     * 40903,电话号码重复<br>
     * 40904,邮箱重复
     *
     * @param checkAccountDTO
     * @author: chenac
     * @date: 15:42
     * @execption:
     * @return: void
     */
    @PutMapping(value = "/check-account/v1")
    @ApiOperation(value = "检测用户账号")
    @ApiResponses({
            @ApiResponse(code = 20000, message = "用户账号正常"),
            @ApiResponse(code = 40000, message = "参数错误"),
            @ApiResponse(code = 40903, message = "电话号码重复"),
            @ApiResponse(code = 40904, message = "邮箱重复")
    })
    public void checkAccount(@RequestBody @Valid CheckAccountDTO checkAccountDTO) {
        manageUserService.checkAccount(checkAccountDTO);
    }

    /**
     * 查询用户列表
     * 查询用户列表 <br>
     * 20000,查询用户列表成功 <br>
     * 50000,服务端异常
     *
     * @param listUserParamDTO
     * @author: chenac
     * @date: 16:25
     * @execption:
     * @return: com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO<com.happok.onlineclass.usermanagement.user.dto.ListUserDTO>
     */
    @PostMapping(value = "/batch/v1")
    @ApiOperation(value = "查询用户列表")
    @ApiResponses({
            @ApiResponse(code = 20000, message = "查询用户列表成功"),
            @ApiResponse(code = 50000, message = "服务端异常")
    })
    public PageResponse<ListUserDTO> listUser(@RequestBody ListUserParamDTO listUserParamDTO) throws URISyntaxException {
        return manageUserService.listUser(listUserParamDTO);
    }

    /**
     * 用户id列表查询用户信息
     * 用户id列表查询用户 <br>
     * 20000,查询用户列表成功 <br>
     * 50000,服务端异常
     *
     * @param userIds
     * @author: chenac
     * @date: 16:25
     * @execption:
     * @return: com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO<com.happok.onlineclass.usermanagement.user.dto.ListUserDTO>
     */
    @GetMapping(value = "/batch/v1")
    @ApiOperation(value = "用户id列表查询用户信息")
    @ApiResponses({
            @ApiResponse(code = 20000, message = "查询用户列表成功"),
            @ApiResponse(code = 50000, message = "服务端异常")
    })
    public List<ListUserDTO> listUserBasicInfo(@RequestParam(value = "userIds", required = true) List<Integer> userIds) {
        return manageUserService.listUserBasicInfo(userIds);
    }

    /**
     * 查询用户详情
     * 查询用户详情 <br>
     * 20000，平台查询用户详情成功 <br>
     * 40402，用户不存在 <br>
     * 50000，服务端异常
     *
     * @param userId 查询的用户id
     * @author: chenac
     * @date: 15:10
     * @execption:
     * @return: com.happok.onlineclass.usermanagement.user.dto.UserDetailsDTO
     */
    @GetMapping(value = "/{userId}/v1")
    @ApiOperation(value = "查询用户详情")
    @ApiResponses({
            @ApiResponse(code = 20000, message = "查询用户详情成功"),
            @ApiResponse(code = 40402, message = "用户不存在"),
            @ApiResponse(code = 50000, message = "服务端异常")
    })
    public UserDetailDTO getUserDetailById(@PathVariable("userId") Integer userId) throws URISyntaxException {
        return manageUserService.getUserDetailById(userId);
    }

    /**
     * 重置密码
     * 管理员通过此接口重置用户密码，如若邮箱以及短信配置有问题则密码必须输入<br>
     * 20000,重置密码成功<br>
     * 20010,通知发送失败<br>
     * 40000,参数异常<br>
     * 40402,用户不存在<br>
     * 50000,服务端异常
     *
     * @param resetPwdDTO
     * @author: chenac
     * @date: 12:12
     * @execption:
     * @return: void
     */
    @PutMapping(value = "/reset/password/v1")
    @ApiOperation(value = "重置密码")
    @ApiResponses({
            @ApiResponse(code = 20000, message = "重置密码成功"),
            @ApiResponse(code = 20010, message = "通知发送失败"),
            @ApiResponse(code = 40000, message = "参数异常"),
            @ApiResponse(code = 40402, message = "用户不存在"),
            @ApiResponse(code = 50000, message = "服务端异常")
    })
    public void resetPassword(@RequestBody @Valid ResetPwdDTO resetPwdDTO) {
        manageUserService.resetPassword(resetPwdDTO);
    }
}